﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Data.Common;
using System.Text;

namespace BtoBSWT315.Ascx
{
    public partial class Search_Product : System.Web.UI.UserControl
    {
        public string KeyWords { get { return Request.Form["Header_Default1:SearchKeyWords"]; } }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!"Product".Equals(Request.Form["Header_Default1:SearchCategory"]))
            {
                this.Visible = false;
                return;
            }
            this.Visible = true;
            Console.Out.WriteLine(KeyWords);
            DataTable dt = GetSearchList(KeyWords);
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();

            if (dt != null)  //TODO 当用户登录，输入用户名和密码时，直接按回车键，会出现DT=NULL
            {
                if (dt.Rows.Count > 15)
                {
                    ViewState["SearchProductResult"] = dt;
                }
            }
        }


        public DataTable GetSearchList(string keyWords)
        {
            keyWords = keyWords.Trim();
            if (keyWords == "")
            {
                return null;
            }
            string sql = "SELECT id, cpmc, jysm, sccj from spzs where ";
            List<DbParameter> parameters = new List<DbParameter>();
            if (keyWords.Contains(" "))
            {
                string[] tmp = keyWords.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                if (tmp != null)
                {
                    StringBuilder sqlWhere = new StringBuilder();
                    int i = 0;

                    foreach (string s in tmp)
                    {
                        if (s == "")
                        {
                            continue;
                        }
                        if (i > 0)
                        {
                            sqlWhere.Append(" AND ");
                        }
                        sqlWhere.Append(" (cpmc like @keyword" + i + " or jysm like @keyword" + i + " or xxsm like @keyword" + i + ")");
                        OleDbParameter param = new OleDbParameter("@keyword" + i, OleDbType.VarChar);
                        param.Value = "%" + s + "%";
                        parameters.Add(param);
                    }

                    sql = sql + sqlWhere;
                }
            }
            else
            {
                sql = sql + string.Format(" (cpmc like @keyword or jysm like @keyword or xxsm like @keyword)");
                OleDbParameter param = new OleDbParameter("@keyword", OleDbType.VarChar);
                param.Value = "%" + keyWords.Trim() + "%";
                parameters.Add(param);

            }
            //引入数据库操作类
            Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();


            return classdb.GetTables(sql, parameters.ToArray());
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (ViewState["SearchProductResult"] == null)
            {
                Response.Write("<script language='JavaScript'>alert('页面不存在或已删除！');location.href='Default.aspx';</script>");
                return;
            }

            DataTable NewsTable = (DataTable)ViewState["NewsTable"];
            GridView1.DataSource = NewsTable;
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
            if (GridView1.Rows.Count < 15)
            {
                GridView1.Height = 18 * GridView1.Rows.Count;
            }
        }
    }
}